package com.xof.phonecall.component.navigation

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.xof.phonecall.R
import com.xof.phonecall.extipetrpide.getParentActivityipetrpide
import com.xof.phonecall.extipetrpide.safeipetrpide

/**
<attr name="tnbCenterTextColor" format="color" />
<attr name="tnbCenterText" format="string" />
<attr name="tnbLeftImgRes" format="reference" />
<attr name="tnbRightImgRes" format="reference" />
<attr name="tnbLeftHidden" format="boolean" />
<attr name="tnbRightHidden" format="boolean" />
 * */
class TopNavigationBaripetrpide : RelativeLayout {

    private lateinit var tvNavbarCenterTitleipetrpide: TextView
    private lateinit var llNavbarLeftipetrpide: LinearLayout
    private lateinit var ivNavbarLeftipetrpide: ImageView
    private lateinit var llNavbarRightipetrpide: LinearLayout
    private lateinit var ivNavbarRightipetrpide: ImageView
    private lateinit var mNavbarRootView: View

    private var tnbCenterTextColoripetrpide: Int = 0
    private var tnbCenterTextipetrpide: String? = null
    private var tnbLeftImgResipetrpide: Int = 0
    private var tnbRightImgResipetrpide: Int = 0
    private var tnbRightHiddenipetrpide: Boolean = true
    private var tnbLeftHiddenipetrpide: Boolean = false
    private var tnbLeftActionModeipetrpide: Int = 0
    private var tnbRightActionModeipetrpide: Int = 0

    private var mSupportFragmentManageripetrpide: FragmentManager? = null

    private var mOnActionClickListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let {
                val array = it.obtainStyledAttributes(attrs, R.styleable.TopNavigationBaripetrpide)
                tnbCenterTextColoripetrpide = array.getColor(
                    R.styleable.TopNavigationBaripetrpide_tnbCenterTextColor,
                    ContextCompat.getColor(context, R.color.black_1)
                )
                tnbCenterTextipetrpide =
                    array.getString(R.styleable.TopNavigationBaripetrpide_tnbCenterText)
                tnbLeftImgResipetrpide =
                    array.getResourceId(R.styleable.TopNavigationBaripetrpide_tnbLeftImgRes, 0)
                tnbRightImgResipetrpide =
                    array.getResourceId(R.styleable.TopNavigationBaripetrpide_tnbRightImgRes, 0)
                tnbLeftHiddenipetrpide =
                    array.getBoolean(R.styleable.TopNavigationBaripetrpide_tnbLeftHidden, false)
                tnbRightHiddenipetrpide =
                    array.getBoolean(R.styleable.TopNavigationBaripetrpide_tnbRightHidden, true)

                tnbLeftActionModeipetrpide = array.getInt(
                    R.styleable.TopNavigationBaripetrpide_tnbLeftActionMode,
                    R.integer.ActionModeFinish
                )
                tnbRightActionModeipetrpide = array.getInt(
                    R.styleable.TopNavigationBaripetrpide_tnbRightActionMode,
                    R.integer.ActionModeCustomerService
                )
                array.recycle()
            }
        }
        context?.let {
            mNavbarRootView = inflate(it, R.layout.view_top_navigation, this)
            initSubViewsipetrpide()
        }
    }

    private fun initSubViewsipetrpide() {
        tvNavbarCenterTitleipetrpide = mNavbarRootView.findViewById(R.id.tv_navbar_center_title)
        llNavbarLeftipetrpide = mNavbarRootView.findViewById(R.id.ll_navbar_left)
        ivNavbarLeftipetrpide = mNavbarRootView.findViewById(R.id.iv_navbar_left)
        llNavbarRightipetrpide = mNavbarRootView.findViewById(R.id.ll_navbar_right)
        ivNavbarRightipetrpide = mNavbarRootView.findViewById(R.id.iv_navbar_right)

        tvNavbarCenterTitleipetrpide.text = tnbCenterTextipetrpide?.safeipetrpide()
        tvNavbarCenterTitleipetrpide.setTextColor(tnbCenterTextColoripetrpide)
        ivNavbarLeftipetrpide.setImageResource(tnbLeftImgResipetrpide)
        ivNavbarRightipetrpide.setImageResource(tnbRightImgResipetrpide)

        if (tnbLeftHiddenipetrpide)
            llNavbarLeftipetrpide.visibility = View.GONE
        else
            llNavbarLeftipetrpide.visibility = View.VISIBLE

        if (tnbRightHiddenipetrpide)
            llNavbarRightipetrpide.visibility = View.GONE
        else
            llNavbarRightipetrpide.visibility = View.VISIBLE

        llNavbarLeftipetrpide.setOnClickListener {

            when (tnbLeftActionModeipetrpide) {
                resources.getInteger(R.integer.ActionModeClick) -> {
                    mOnActionClickListener.invoke(R.integer.NavbarActionLeft)
                }
                resources.getInteger(R.integer.ActionModeFinish) -> {
                    val gActivity = getParentActivityipetrpide()
                    gActivity?.finish()
                }
                resources.getInteger(R.integer.ActionModeToRoot) -> {
//                    val gActivity = getParentActivityipetrpide()
//                    gActivity?.startToHomePageipetrpide()
                }
            }
        }

        llNavbarRightipetrpide.setOnClickListener {
            when (tnbRightActionModeipetrpide) {
                resources.getInteger(R.integer.ActionModeClick) -> {
                    mOnActionClickListener.invoke(R.integer.NavbarActionRight)
                }
                resources.getInteger(R.integer.ActionModeCustomerService) -> {
//                    mSupportFragmentManageripetrpide?.let {
//                        CustomServiceDialog.showDialog(it)
//                        return@setOnClickListener
//                    }
//
//                    val gActivity = getParentActivityipetrpide()
//                    gActivity?.let {
//                        CustomServiceDialog.showDialog(it.supportFragmentManager)
//                    }
                }
            }
        }
    }

    fun setFragmentManageripetrpide(manager: FragmentManager) {
        mSupportFragmentManageripetrpide = manager
    }

    fun setOnActionClickListener(listener: (Int) -> Unit) {
        mOnActionClickListener = listener
    }

    fun setLeftImgRes(imgRes: Int) {
        ivNavbarLeftipetrpide.setImageResource(imgRes)
    }

    fun setCenterText(centerText: String) {
        tnbCenterTextipetrpide = centerText
        tvNavbarCenterTitleipetrpide.text = (centerText)
    }
}